package com.daka.service.impl;

import com.daka.dao.VolunteerDao;
import com.daka.dao.VolunteerMoneyDao;
import com.daka.dao1.MemberMoneyRecordDao;
import com.daka.entity.MemberMoneyRecord;
import com.daka.entity.Volunteer;
import com.daka.entity.VolunteerMoney;
import com.daka.service.VolunteerMoneyService;
import com.daka.tools.ConfigUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/7/25 0025
 * Time: 11:24
 */
@Service("volunteerMoneyService")
public class VolunteerMoneyServiceImpl implements VolunteerMoneyService {

    @Resource
    private MemberMoneyRecordDao memberMoneyRecordDao;

    @Resource
    private VolunteerDao volunteerDao;

    @Resource
    private VolunteerMoneyDao volunteerMoneyDao;

    public void migrate(Integer max,Integer min) {

        List<MemberMoneyRecord> memberMoneyRecordList;
        if(max == 0 || min == 0){
            memberMoneyRecordList = memberMoneyRecordDao.selectAll();
        }else{
            memberMoneyRecordList = memberMoneyRecordDao.selectByIdBetween(max, min);
        }

        List<VolunteerMoney> volunteerMoneyList = new ArrayList<VolunteerMoney>();

        for (MemberMoneyRecord memberMoneyRecord : memberMoneyRecordList) {

            Volunteer volunteer = volunteerDao.selectByOldId(memberMoneyRecord.getMember());

            volunteerMoneyList.add(VolunteerMoney.build(memberMoneyRecord, volunteer.getId().intValue()));

            if (volunteerMoneyList.size() >= ConfigUtil.BATCH_INSERT_MAX) {
                int i = volunteerMoneyDao.insertBatch(volunteerMoneyList);

                if (i > 0) {
                    volunteerMoneyList.clear();
                }
            }
        }

        if (volunteerMoneyList.size() > 0) {
            volunteerMoneyDao.insertBatch(volunteerMoneyList);
        }
    }
}
